package com.chookss.home.logContacts;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.entity.LogEntity;
import com.chookss.mine.personal.MyLogActivity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;
import com.chookss.tools.bigImage.ImagePreview;
import com.chookss.tools.bigImage.bean.ImageInfo;
import com.johnrambo.ktea.common.klogutil.KLog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LogImagsAdapter extends BaseQuickAdapter<String, BaseViewHolderEx> {
    private Context context;
    private int width;
    private int index = -1;//开始都是视频
    private int position = -1;//开始都是视频
    private LogEntity logEntity;
    private List<ImageInfo> imageInfoList = new ArrayList<>();

    public LogImagsAdapter(int layoutResId, Context context, @io.reactivex.annotations.Nullable List<String> data, int index, LogEntity logEntity, int position) {
        super(layoutResId, data);
        this.context = context;
        this.index = index;
        this.position = position;
        this.logEntity = logEntity;
        width = (Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 98)) / 4;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, String item) {

        ImageView ivBg = helper.getView(R.id.ivBg);
        ImageView ivVideo = helper.getView(R.id.ivVideo);
        ImageView ivBf = helper.getView(R.id.ivBf);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivBg.getLayoutParams();
        params.width = width;
        params.height = width;
        ivBg.setLayoutParams(params);
        ivVideo.setLayoutParams(params);
        if (index >= 0) {
            if (helper.getItemPosition() >=index ) {
                //视频
                ivBg.setVisibility(View.GONE);
                ivVideo.setVisibility(View.VISIBLE);
                GlideUtils.load(context, item, ivVideo, R.drawable.default_news);
                ivBf.setVisibility(View.VISIBLE);
            } else {
                ivBg.setVisibility(View.VISIBLE);
                ivVideo.setVisibility(View.GONE);
                GlideUtils.load(context, item, ivBg, R.drawable.default_news);
                ivBf.setVisibility(View.GONE);
            }
        } else {
            ivBg.setVisibility(View.VISIBLE);
            ivVideo.setVisibility(View.GONE);
            GlideUtils.load(context, item, ivBg, R.drawable.default_news);
            ivBf.setVisibility(View.GONE);
        }
        ivBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPicUrlList(helper.getItemPosition());
            }
        });
        ivVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position >= 0) {
                    HashMap<String, String> map = new HashMap<>();
                    map.put("Log_Read", position + "");
                    EventBus.getDefault().post(new MyEvent(map));
                }
                Intent intent = new Intent(context, LogDetailActivity.class);
                intent.putExtra("data", logEntity);
                intent.putExtra("position", helper.getItemPosition());
                context.startActivity(intent);
            }
        });
    }

    private void setPicUrlList(int position) {
        List<ImageInfo> imageInfoList = new ArrayList<>();
        for (int i = 0; i < getData().size(); i++) {
            ImageInfo imageInfo = new ImageInfo();
            if (getData().get(i).contains("x-oss-process")) {
                imageInfo.setThumbnailUrl(getData().get(i).split("\\?")[0]);// 缩略图url
                imageInfo.setOriginUrl(getData().get(i).split("\\?")[0]);// 原图url
            } else {
                imageInfo.setOriginUrl(getData().get(i));// 原图url
                imageInfo.setThumbnailUrl(getData().get(i));// 缩略图url
            }
            imageInfoList.add(imageInfo);
        }
        ImagePreview
                .getInstance()
                // 上下文，必须是activity，不需要担心内存泄漏，本框架已经处理好；
                .setContext(context)
                // 设置从第几张开始看（索引从0开始）
                .setIndex(position)
                .setShowCloseButton(true)
                .setLoadStrategy(ImagePreview.LoadStrategy.NetworkAuto)
                .setImageInfoList(imageInfoList)
                .start();
    }

}
